package com.ssm.service.impl;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.mapper.CaseObjMapper;
import com.ssm.model.CaseObj;
import com.ssm.service.CaseObjService;
import com.ssm.utils.CaseUtils;

@Service
public class CaseObjServiceImpl implements CaseObjService {

	private Logger logger = Logger.getLogger(CaseObjServiceImpl.class);
	
	@Autowired
	private CaseObjMapper caseObjMapper;
	
	/**
	 * 四种响应结果：
	 * 全部上传成功{"success":true, "message":"全部上传成功"}
	 * 部分上传成功{"success":true, "message":"部分上传成功", "detail":[]}
	 * 全部上传失败{"success":false, "message":"部分上传成功", "detail":[]}
	 * 文件格式错误{"success":false, "message":"文件格式有误"}
	 */
	@Override
	public JSONObject importFromExcel(File excelFile, String product, String version) {
		
		JSONObject res = new JSONObject();
		
		JSONArray caseArray = CaseUtils.analyseCaseExcel(excelFile);
		
		if (caseArray == null) {
			res.put("success", false);
			res.put("message", "文件内容或格式有误");
			return res;
		} else if (caseArray.size() == 0) {
			res.put("success", false);
			res.put("message", "excel中用例数为0");
			return res;
		}
		
		// 创建一个JSONArray用来保存插入失败的用例
		JSONArray fail = new JSONArray();
		// 将用例一条一条的插入数据库
		for (Object object : caseArray) {
			CaseObj caseObj = (CaseObj) object;
			
			try {
				caseObjMapper.insert(product, version, caseObj);
			} catch (Exception e) {
				
				JSONObject tempFailCase = new JSONObject();
				if (e.getMessage().contains("Duplicate entry")) {
					tempFailCase.put("fail_reason", "case_num重复");
				} else {
					tempFailCase.put("fail_reason", e.getMessage());
				}
				tempFailCase.put("case_num", caseObj.getNum());
				tempFailCase.put("case_name", caseObj.getName());
				
				fail.add(tempFailCase);
			}
		}
		
		if (fail.size() == 0) {
			res.put("success", true);
			res.put("message", "全部上传成功，共：" + caseArray.size() + "条用例");
			return res;
		} else if (fail.size() == caseArray.size()) {
			res.put("success", false);
			res.put("message", "全部上传失败");
			res.put("detail", fail);
			return res;
		} else {
			res.put("success", true);
			res.put("message", "部分上传成功，成功：" + (caseArray.size() - fail.size()) + "，失败：" + fail.size());
			res.put("detail", fail);
			return res;
		}
	}

	@Override
	public JSONObject exportToExcel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject getCaseBaseInfo(JSONObject condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject getCaseObjByNum(String casenum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject modifyCase(JSONObject caseobj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject addCase(JSONObject caseobj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject delCase(String casenum) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public JSONObject getZTreeData(JSONObject paramJson) {
		
		JSONObject res = new JSONObject();
		
		List<CaseObj> caseList = caseObjMapper.selectSubinfo(paramJson.getString("product"), paramJson.getString("version"));
		
		// 这里根据过滤条件对case进行一下筛选，生成一个新的List
		
		JSONArray zTreeData = CaseUtils.analyseToZNodes(caseList);
		
		if (zTreeData == null || zTreeData.size() == 0) {
			res.put("success", false);
			res.put("message", "查询无数据");
			return res;
		} else {
			res.put("success", true);
			res.put("message", "查询到" + caseList.size() + "条用例数据");
			res.put("detail", zTreeData);
			return res;
		}
	}

	@Override
	public JSONObject getCaseDetail(JSONObject paramJson) {
		
		JSONObject res = new JSONObject();
		
		try {
			CaseObj caseObj = caseObjMapper.selectByCaseNum(paramJson.getString("product"), paramJson.getString("version"), paramJson.getString("num"));
			if (caseObj != null) {
				res.put("success", true);
				res.put("detail", caseObj);
			} else {
				res.put("success", false);
				res.put("message", "数据库中不存在该用例");
			}
		} catch (Exception e) {
			res.put("success", false);
			res.put("message", e.getMessage());
		}
		
		return res;
	}
}
